namespace DotNetNuke.Common.Utilities
{
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.IO;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using System.Xml.Xsl;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The XmlUtils class provides Shared/Static methods for manipulating xml files
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	11/08/2004	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class XmlUtils
    {
        public static void AppendElement(ref XmlDocument objDoc, XmlNode objNode, string attName, string attValue, bool includeIfEmpty)
        {
            if (!((attValue == "") & !includeIfEmpty))
            {
                objNode.AppendChild(CreateElement(objDoc, attName, attValue));
            }
        }

        public static XmlAttribute CreateAttribute(XmlDocument objDoc, string attName, string attValue)
        {
            XmlAttribute attribute = objDoc.CreateAttribute(attName);
            attribute.Value = attValue;
            return attribute;
        }

        public static XmlElement CreateElement(XmlDocument objDoc, string NodeName, string NodeValue)
        {
            XmlElement element = objDoc.CreateElement(NodeName);
            element.InnerText = NodeValue;
            return element;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of node
        /// </summary>
        /// <param name="objNode">Parent node</param>
        /// <param name="NodeName">Child node to look for</param>
        /// <param name="DefaultValue">Default value to return</param>
        /// <returns></returns>
        /// <remarks>
        /// If the node does not exist or it causes any error the default value will be returned.
        /// </remarks>
        /// <history>
        /// [VMasanas]	09/09/2004	Created
        /// [cnurse]	11/08/2004	moved from PortalController and made Public Shared
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string GetNodeValue(XmlNode objNode, string NodeName, [Optional, DefaultParameterValue("")] string DefaultValue)
        {
            string strValue = DefaultValue;
            try
            {
                strValue = objNode[NodeName].InnerText;
                if ((strValue == "") & (DefaultValue != ""))
                {
                    strValue = DefaultValue;
                }
            }
            catch (Exception exception1)
            {
                
                
            }
            return strValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of node
        /// </summary>
        /// <param name="objNode">Parent node</param>
        /// <param name="NodeName">Child node to look for</param>
        /// <param name="DefaultValue">Default value to return</param>
        /// <returns></returns>
        /// <remarks>
        /// If the node does not exist or it causes any error the default value will be returned.
        /// </remarks>
        /// <history>
        /// [VMasanas]	09/09/2004	Added new method to return converted values
        /// [cnurse]	11/08/2004	moved from PortalController and made Public Shared
        /// </history>
        /// -----------------------------------------------------------------------------
        public static bool GetNodeValueBoolean(XmlNode objNode, string NodeName, [Optional, DefaultParameterValue(false)] bool DefaultValue)
        {
            bool bValue = DefaultValue;
            try
            {
                bValue = Convert.ToBoolean(objNode[NodeName].InnerText);
            }
            catch (Exception exception1)
            {
                
                
            }
            return bValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of node
        /// </summary>
        /// <param name="objNode">Parent node</param>
        /// <param name="NodeName">Child node to look for</param>
        /// <param name="DefaultValue">Default value to return</param>
        /// <returns></returns>
        /// <remarks>
        /// If the node does not exist or it causes any error the default value will be returned.
        /// </remarks>
        /// <history>
        /// [VMasanas]	09/09/2004	Added new method to return converted values
        /// [cnurse]	11/08/2004	moved from PortalController and made Public Shared
        /// </history>
        /// -----------------------------------------------------------------------------
        public static DateTime GetNodeValueDate(XmlNode objNode, string NodeName, DateTime DefaultValue)
        {
            DateTime dateValue = DefaultValue;
            try
            {
                if (objNode[NodeName].InnerText == "")
                {
                    return dateValue;
                }
                dateValue = Convert.ToDateTime(objNode[NodeName].InnerText);
                if (dateValue.Date.Equals(Null.NullDate.Date))
                {
                    dateValue = Null.NullDate;
                }
            }
            catch (Exception exception1)
            {
                
                
            }
            return dateValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of node
        /// </summary>
        /// <param name="objNode">Parent node</param>
        /// <param name="NodeName">Child node to look for</param>
        /// <param name="DefaultValue">Default value to return</param>
        /// <returns></returns>
        /// <remarks>
        /// If the node does not exist or it causes any error the default value will be returned.
        /// </remarks>
        /// <history>
        /// [VMasanas]	09/09/2004	Added new method to return converted values
        /// [cnurse]	11/08/2004	moved from PortalController and made Public Shared
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int GetNodeValueInt(XmlNode objNode, string NodeName, [Optional, DefaultParameterValue(0)] int DefaultValue)
        {
            int intValue = DefaultValue;
            try
            {
                intValue = Convert.ToInt32(objNode[NodeName].InnerText);
            }
            catch (Exception exception1)
            {
                
                
            }
            return intValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value of node
        /// </summary>
        /// <param name="objNode">Parent node</param>
        /// <param name="NodeName">Child node to look for</param>
        /// <param name="DefaultValue">Default value to return</param>
        /// <returns></returns>
        /// <remarks>
        /// If the node does not exist or it causes any error the default value will be returned.
        /// </remarks>
        /// <history>
        /// [VMasanas]	09/09/2004	Added new method to return converted values
        /// [cnurse]	11/08/2004	moved from PortalController and made Public Shared
        /// </history>
        /// -----------------------------------------------------------------------------
        public static float GetNodeValueSingle(XmlNode objNode, string NodeName, [Optional, DefaultParameterValue(0f)] float DefaultValue)
        {
            float sValue = DefaultValue;
            try
            {
                sValue = Convert.ToSingle(objNode[NodeName].InnerText);
            }
            catch (Exception exception1)
            {
                
                
            }
            return sValue;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetXMLContent loads the xml content into an Xml Document
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ContentUrl">The url to the xml text</param>
        /// <returns>An XmlDocument</returns>
        /// <history>
        /// [cnurse]	9/23/2004	Moved XML to a separate Project
        /// [cnurse]    1/21/2005   Moved to XmlUtils class
        /// </history>
        /// -----------------------------------------------------------------------------
        public static XmlDocument GetXMLContent(string ContentUrl)
        {
            XmlDocument GetXMLContent = new XmlDocument();
            WebResponse result = WebRequest.Create(ContentUrl).GetResponse();
            Stream ReceiveStream = result.GetResponseStream();
            XmlReader objXmlReader = new XmlTextReader(result.GetResponseStream());
            GetXMLContent.Load(objXmlReader);
            return GetXMLContent;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetXSLContent loads the xsl content into an Xsl Transform
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="ContentUrl">The url to the xsl text</param>
        /// <returns>An XslTransform</returns>
        /// <history>
        /// [cnurse]	9/23/2004	Moved XML to a separate Project
        /// [cnurse]    1/21/2005   Moved to XmlUtils class
        /// </history>
        /// -----------------------------------------------------------------------------
        public static XslTransform GetXSLContent(string ContentURL)
        {
            XslTransform GetXSLContent = new XslTransform();
            WebResponse result = WebRequest.Create(ContentURL).GetResponse();
            Stream ReceiveStream = result.GetResponseStream();
            XmlReader objXSLTransform = new XmlTextReader(result.GetResponseStream());
            GetXSLContent.Load(objXSLTransform, null, null);
            return GetXSLContent;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Serializes an object to Xml using the XmlAttributes
        /// </summary>
        /// <param name="obj">The object to Serialize</param>
        /// <returns></returns>
        /// <remarks>
        /// An Xml representation of the object.
        /// </remarks>
        /// <history>
        /// [cnurse]	11/25/2004	created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string Serialize(object obj)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlSerializer xser = new XmlSerializer(obj.GetType());
            StringWriter sw = new StringWriter();
            xser.Serialize((TextWriter) sw, RuntimeHelpers.GetObjectValue(obj));
            xmlDoc.LoadXml(sw.GetStringBuilder().ToString());
            XmlNode xmlDocEl = xmlDoc.DocumentElement;
            xmlDocEl.Attributes.Remove(xmlDocEl.Attributes["xmlns:xsd"]);
            xmlDocEl.Attributes.Remove(xmlDocEl.Attributes["xmlns:xsi"]);
            return xmlDocEl.OuterXml;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Xml Encodes HTML
        /// </summary>
        /// <param name="HTML">The HTML to encode</param>
        /// <returns></returns>
        /// <history>
        /// [cnurse]	09/29/2005	moved from Globals
        /// </history>
        /// -----------------------------------------------------------------------------
        public static string XMLEncode(string HTML)
        {
            return ("<![CDATA[" + HTML + "]]>");
        }

        public static void XSLTransform(XmlDocument doc, ref StreamWriter writer, string xsltUrl)
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(xsltUrl);
            xslt.Transform((IXPathNavigable) doc, null, writer);
        }
    }
}

